Implement an association
As next, the one-to-many association between the Employees and the Orders is implemented.
Extend the Employee class with a new property OrderList and a new method LoadOrders:
public partial class Employee { private IList<Orders> orderList; /// <summary> /// All orders of the Employee. /// </summary> public IList<Orders> OrderList { get { if (this.orderList == null) this.LoadOrders(); return this.orderList; } } /// <summary> /// Loads all Orders of the Employee. /// </summary> public void LoadOrders() { this.orderList = Orders.FindByEmployeeid(this.employeeid); } |
Remark: Normally this property would be named Orders, but this is already the name of the database table Orders (You can change the name of the class in the Details tab to Order).
We can use the generated method FindByEmployeeid here, so there is no additional command needed.
By using this implementation, we will get a lazy loading effect: The orders are only loaded when they are requested in code.
To get the latest orders from the database, you can always refresh the OrderList property by calling the LoadOrders method.
In the next section you can create a sample presentation layer that will use the functionality of the business logic layer.